package com.ytzl.demo.controller;

import com.ytzl.common.CodeResultEnums;
import com.ytzl.common.R;
import com.ytzl.common.ResponseDataUtils;
import com.ytzl.modul.ItripHotel;
import com.ytzl.vo.HotCityHoteVO;
import com.ytzl.vo.SearchHotCityVO;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.annotation.Resources;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@RestController
@RequestMapping("/api/testhotellist")
public class TestHotelListController {

    @Resource
    private SolrClient solrClient;

    @PostMapping("/searchItripHotelListByHotCity")
    public R searchHotelListByList(SearchHotCityVO hotCityVO) throws IOException, SolrServerException {
        //检查参数是否存在
        if (hotCityVO==null||hotCityVO.getCityId()==null){
            return ResponseDataUtils.buildError(CodeResultEnums.CITY_ID_IS_NULL);
        }
        //根据城市id查询酒店列表：基于solr查询  ->配置solr
        List<HotCityHoteVO> hotCityHoteVOList=this.solrQueryHotCityHotelList(hotCityVO);
        return ResponseDataUtils.buildSuccess(hotCityHoteVOList);
    }
    public List<HotCityHoteVO> solrQueryHotCityHotelList(SearchHotCityVO hoteVO) throws IOException, SolrServerException {
        HotCityHoteVO hotCityHoteVO =new HotCityHoteVO();
        ItripHotel itripHotel=new ItripHotel();
        List<HotCityHoteVO> hoteVOList=new ArrayList<HotCityHoteVO>();
        //solr的查询条件构造对象
        SolrQuery query=new SolrQuery();
        //默认查所有
        query.setQuery("*:*");
        //设置其他查询条件
        query.setFilterQueries("cityId:"+hoteVO.getCityId());
        //分页
        query.setStart(0);
        query.setRows(hoteVO.getCount());
        //查询结果的封装对象
        QueryResponse response=solrClient.query(query);
//        //获取查询信息
//        SolrDocumentList results=response.getResults();
//        //查询到的数量
//        Long numFound=results.getNumFound();

//        //遍历查询结果
//        for (SolrDocument result:results){
//            Collection<String> fieldNames=result.getFieldNames();
////            hotCityHoteVO.setCityid((Long) result.get(itripHotel.getCityid()));
////            hotCityHoteVO.setHotelname(result.get("hotelName").toString());
//
//            //将solr查出来的结果转换成Java对象
//            hotCityHoteVO=solrClient.getBinder().getBean(HotCityHoteVO.class,result);
//            //将查询结果添加到集合
//            hoteVOList.add(hotCityHoteVO);
//        }
//        return hoteVOList;
        return response.getBeans(HotCityHoteVO.class);
    }
}
